$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'QuestionBank/Select',
        datatype: "json",
        colModel: [
            { label: '题目ID', name: 'questionId', width: 40,key: true },
             { label: '题目名称', name: 'questionName', width: 40 },
             { label: '选项A', name: 'choiceA', width: 40 },
             { label: '选项B', name: 'choiceB', width: 40 },
             { label: '选项C', name: 'choiceC', width: 40 },
             { label: '选项D', name: 'choiceD', width: 40 },
             { label: '题目类型', name: 'questionType', width: 40 },
             { label: '题目学科', name: 'questionSubject', width: 40 }
         ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        QuestionBank: {},
        questionId: null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.QuestionBank = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "QuestionBank/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.QuestionBank = r.QuestionBank;
                vm.questionId = r.QuestionBank.questionId;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "QuestionBank/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.QuestionBank.questionId == vm.questionId ? "QuestionBank/Update" : "QuestionBank/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.QuestionBank),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'questionId': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.QuestionBank.questionId)){
                alert("题目ID不能为空");
                return true;
            }
            if(isBlank(vm.QuestionBank.questionName)){
                alert("题目名称不能为空");
                return true;
            }
            if(isBlank(vm.QuestionBank.choiceA)){
                alert("选项A不能为空");
                return true;
            }
            if(isBlank(vm.QuestionBank.choiceB)){
                alert("选项B不能为空");
                return true;
            }
            if(isBlank(vm.QuestionBank.choiceC)){
                alert("选项C不能为空");
                return true;
            }
            if(isBlank(vm.QuestionBank.choiceD)){
                alert("选项D不能为空");
                return true;
            }
            if(isBlank(vm.QuestionBank.questionType)){
                alert("题目类型不能为空");
                return true;
            }
            if(isBlank(vm.QuestionBank.questionSubject)){
                alert("题目学科不能为空");
                return true;
            }
            }
        }
});